import user_agent_get
import requests
from time import sleep
from urllib.parse import urlencode
import re
from pyquery import PyQuery as pq

tongyi=user_agent_get.user_agent_get()
session=requests.Session()
url='http://jw.pxc.jx.cn/jsxsd/sso.jsp'
headers={"User-Agent": tongyi,"Host":"jw.pxc.jx.cn"
         ,'Referer':'http://portal.pxc.jx.cn/'}
# session.cookies.update({
# '_gscu_93803120':"367153693ncfaf63", '_gscbrs_93803120':'1', 'SERVERID':'123', 'JSESSIONID':'8A6929BCDD4DF28A5CD4CD3563157B01'
#
# })
getfirstsession=session.get(url,headers=headers,allow_redirects=False)
getfirstsession.encoding='utf-8'
print(getfirstsession.text)
print("++++++++++++++++++++++")
print("getsession : "+str(getfirstsession.status_code))
print(session.cookies.get_dict())
sesDict=session.cookies.get_dict()



patt=re.compile("href='(.*?)'<")
print(getfirstsession.text)
# url=patt.findall(getfirstsession.text)[0]
url=getfirstsession.headers['Location']

session2=requests.Session()
headers2={'User-Agent':tongyi}

resp=session2.get(url,headers=headers)


tempPQ=pq(resp.text)
tempPQStr=tempPQ("input[type='hidden'][name='lt']").attr("value")
tempPQStr22=tempPQ("input[type='hidden'][name='execution']").attr("value")


imgUrl="https://cas.pxc.jx.cn/lyuapServer/captcha.jsp"
imgResp=session2.get(imgUrl,headers=headers)
with open(r'爬虫信息/a2.jpg','wb') as f:
    f.write(imgResp.content)

myName=input("输入学号：")
data={
'username': myName,
'password': '000000',
'captcha': '5454',
'warn': 'true',
'lt': 'LT-11972-eoF3b1RwEIudHnOjJTS112wz7HLfWA-cas01.example.org',
'execution': 'e1s1',
'_eventId': 'submit',
'submit': '登录'
}

PostUrl='https://cas.pxc.jx.cn/lyuapServer/login;jsessionid={}?service=http%3A%2F%2Fjw.pxc.jx.cn%2Fjsxsd%2Fsso.jsp'.format(session2.cookies.get_dict()['JSESSIONID'])

str2=input("输入验证码: ")
data.update({'captcha':str2,'lt':tempPQStr,'execution':tempPQStr22})


data=urlencode(data)
print("posturl : "+PostUrl)
print("sessons cookies :",end='  ')
print(session2.cookies.get_dict())

headers2.update({'Referer':'https://cas.pxc.jx.cn/lyuapServer/login?service=http%3A%2F%2Fjw.pxc.jx.cn%2Fjsxsd%2Fsso.jsp',
                 'Origin': 'https://cas.pxc.jx.cn','Host':'cas.pxc.jx.cn',
                 'Sec-Fetch-Site':'same-origin',
                 'Sec-Fetch-Mode':'navigate',
                 'Sec-Fetch-User':'?1',
                'Sec-Fetch-Dest':'document',
                'Content-Type': 'application/x-www-form-urlencoded'
})
pospResp=session2.post(url=PostUrl,headers=headers2,data=data,allow_redirects=False)

print("data : "+data)
print("pospResp:"+str(pospResp.status_code))
print(pospResp.text)
print(pospResp.headers)
print("headers :"+str(headers2))



headers.update({'Host':'jw.pxc.jx.cn'})
url=pospResp.headers['Location']
getUrl2=session.get(url,headers=headers,allow_redirects=False)

print(getUrl2.headers)

sleep(0.5)
url=getUrl2.headers['Location']
getUrl3=session.get(url,headers=headers,allow_redirects=False)

url=getUrl3.headers['Location']
getUrl4=session.get(url,headers=headers,allow_redirects=False)
getUrl4.encoding='utf-8'
print(getUrl4.text)

sleep(0.5)
url='http://jw.pxc.jx.cn/jsxsd/grxx/xsxx'
getUrl5=session.get(url,headers=headers,allow_redirects=False)
getUrl5.encoding='utf-8'
print(getUrl5.text)

with open('E:\commfile\爬虫信息\{}.html'.format(myName), 'w') as f:
    f.write(getUrl5.text)

# sleep(0.5)
# url="http://10.110.1.33:8008/portal/local/index.html?uplcyid=1&weburl=http%3A%2F%2Fjw.pxc.jx.cn%2Fjsxsd%2Fsso.jsp"
# headers={"user-agent":user_agent_get.user_agent_get(),'Host':'10.110.1.33:8008','Referer': 'http://portal.pxc.jx.cn/'}
#
#
# # session.cookies.update({'USGSESSID':'68ebce959fa07130323f43cfe51de8b8'})
# getmessage=session.get(url,headers=headers)
#
# print('getmessage Status :'+str(getmessage.status_code))
# print(getmessage.text)
# print(getmessage.cookies.get_dict())
#
# print('------------------------------------------')
#
# url="http://10.110.1.33:8008/portal.cgi"
# headers.update({'Referer': 'http://10.110.1.33:8008/portal/local/index.html?uplcyid=1&weburl=http%3A%2F%2Fjw.pxc.jx.cn%2Fjsxsd%2Fsso.jsp'
#                 ,"Origin":'http://10.110.1.33:8008'})
#
# data="username=17379930923&password=930923&uplcyid=1&language=0&submit=submit"
# postResp=session.post(url=url,headers=headers,data=data)
# print("postResp Statuscode : "+str(postResp.status_code))
# postResp.encoding='utf-8'
# print(postResp.text)
# print(postResp.cookies.get_dict())

